import java.util.List;

public class AddTask {
	
	Task newTask;  
	//
	
	public AddTask() {
		newTask = new Task();
	}

	public void execute(UserInputElements userInputElements, List<Task> tasksInTempStorage, List<Task> tasksOnDisplay, String typeTasksOnDisplay) {
		String message="";
		if(!this.isAddAllowed(userInputElements)) {
			message = "add is not valid, please provide the title";
		}
		else { 
			this.addToTempStorage(userInputElements,
					tasksInTempStorage, tasksOnDisplay, typeTasksOnDisplay);
			message = "added successfully to the list of tasks";
		}
		System.out.println(message);
		TaskO.taskOMainScreen.appendOutput(message);
	}	
	
	public boolean isAddAllowed(UserInputElements userInputElements) {
		boolean isValid  = true;
		if(userInputElements.getTaskTitle().equals("")) {
		isValid = false;	
		}
		return isValid;
	}

	public void addToTempStorage(UserInputElements userInputElements, List<Task> tasksInTempStorage, List<Task> tasksOnDisplay, String viewTasksType) {
		this.prepareNewTaskObject(userInputElements, tasksInTempStorage.size());
		this.appendNewTaskToList(tasksInTempStorage);
		this.addTaskToCurrentDisplay(tasksInTempStorage, tasksOnDisplay, viewTasksType);
	}

	private void prepareNewTaskObject(UserInputElements userInputElements, int numberOfTasks) {
		int taskIdOfNewTask = numberOfTasks+1;
		this.newTask.setTaskId(taskIdOfNewTask);
		this.newTask.setTaskTitle(userInputElements.getTaskTitle());
		this.newTask.setTaskStartDate(userInputElements.getTaskStartDate());
		this.newTask.setTaskEndDate(userInputElements.getTaskEndDate());
		this.newTask.setTaskDescription(userInputElements.getTaskDescription());
		this.newTask.setIsTaskStarred(userInputElements.getIsTaskStarred()); 
		String temp;
		temp = userInputElements.getTaskType().name();
		this.newTask.setTaskType(temp);
		this.newTask.setIsTaskDone(userInputElements.getIsTaskDone());
	}
	
	private void appendNewTaskToList(List<Task> tasks) {
			tasks.add(this.newTask);	
	}
	
	private void addTaskToCurrentDisplay(List<Task> tasksInTempStorage, List<Task> tasksOnDisplay,
			String typeTasksOnDisplay) {
		//Current display is based on view 'something'
		if(typeTasksOnDisplay.equalsIgnoreCase("/ALL/")) {
		this.appendNewTaskToList(tasksOnDisplay);
		}
		
		else if(typeTasksOnDisplay.equalsIgnoreCase("/STAR/")) {
			boolean isStarred = newTask.getIsTaskStarred();
			if(isStarred) {
			this.appendNewTaskToList(tasksOnDisplay);
			}
		}
		
		else if(typeTasksOnDisplay.equalsIgnoreCase("/DEADLINE/")) {
			String taskType = newTask.getTaskType().name();
			if(taskType.equalsIgnoreCase("DEADLINE")) {
			this.appendNewTaskToList(tasksOnDisplay);
			}
		}
		
		else if(typeTasksOnDisplay.equalsIgnoreCase("/FINISHED/")) {
			boolean isFinished = newTask.getIsTaskDone();
			if(isFinished) {
			this.appendNewTaskToList(tasksOnDisplay);
			}
		}
		
		//Current display is based on search 'something'
		//search if newTask has typeTasksOnDisplay word inside it
		else {
		SearchTask searchTaskFromAdd = new SearchTask();
		searchTaskFromAdd.execute(tasksInTempStorage, tasksOnDisplay, typeTasksOnDisplay);
		}
	}
}